package com.stargate.bd.dao.factory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;

import com.stargate.bd.bean.Affectation;
import com.stargate.bd.bean.Badge;
import com.stargate.bd.bean.Etat;
import com.stargate.bd.dao.DAO;

public class AffectationDAO extends DAO<Affectation> {

	@Override
	public ArrayList<Affectation> selectAll() throws SQLException {
		String sql = "SELECT * FROM " + Affectation.TABLE_NAME;
		ArrayList<Affectation> affectations = new ArrayList<Affectation>();
		Statement statement = connection.createStatement();
		ResultSet result = statement.executeQuery(sql);
		while (result.next()) {
			affectations.add(new Affectation(result.getInt(0),
					result.getInt(1), result.getDate(2), result.getDate(3)));
		}
		result.close();
		statement.close();
		return affectations;
	}

	@Override
	public Affectation select(Affectation bean) throws SQLException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not yet implemented");
	}

	@Override
	public void update(Affectation bean) throws SQLException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not yet implemented");
	}

	@Override
	public void delete(Affectation bean) throws SQLException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not yet implemented");
	}

	@Override
	public void insert(Affectation bean) throws SQLException {
		String sql = "INSERT INTO " + Affectation.TABLE_NAME
				+ " VALUES (?,?,?,?)";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setObject(1, bean.getIdPersonne(), Types.INTEGER);
		statement.setObject(2, bean.getNumBadge(), Types.INTEGER);
		statement.setObject(3, bean.getDateAffectation(), Types.DATE);
		statement.setObject(4, bean.getDateFinAffectation(), Types.DATE);
		statement.executeUpdate();
		statement.close();
	}

	public ArrayList<Badge> selectBadgesPersonne(int idPersonne)
			throws SQLException {
		System.out.println("*** Sélection des badges d'une personne ***");
		String sql = "select b.numbadge, etatbadge " +
				"from badge b join affectation a on b.numbadge=a.numbadge " +
				"where id_personne=" + idPersonne;
		ArrayList<Badge> badges = new ArrayList<Badge>();
		Statement statement = connection.createStatement();
		ResultSet result = statement.executeQuery(sql);
		while (result.next()) {
			badges.add(new Badge(result.getInt(Badge.NUM_BADGE),
					Etat.valueOf(result.getString(Badge.ETAT_BADGE))));
		}
		result.close();
		statement.close();
		return badges;
	}

}
